#include "BlockQueue.hpp"
#include "Task.hpp"
#include <ctime>
#include <cstdlib>

void *Consumer(void *agrs)
{
    BlockQueue<func_t> *bq = static_cast<BlockQueue<func_t> *>(agrs);
    while (1)
    {
        sleep(1);
        // 获取数据
        // int data = 0;
        // Task t;
        func_t t;
        bq->Pop(&t);
        t();
        // 处理数据
        // std::cout << "Consumer -> " << t.result() << std::endl;
    }
}

void *Productor(void *agrs)
{
    BlockQueue<func_t> *bq = static_cast<BlockQueue<func_t> *>(agrs);

    while (1)
    {
        // 构造数据
        // int data = 0;
        // data = rand() % 10 + 1; // [1, 10]

        //    int x = rand() % 10 + 1; // [1, 10]
        //    usleep(10000);
        //    int y = rand() % 10 + 1; // [1, 10]
        // 放入数据
        // Task t(x, y);
        bq->Equeue(DownLode);
        std::cout << "Productor-> " << "DownLode" << std::endl;
    }
}

int main()
{
    srand(time(nullptr) ^ getpid());
    BlockQueue<func_t> *bq = new BlockQueue<func_t>(5);

    pthread_t c, p;
    pthread_create(&c, nullptr, Consumer, bq);
    pthread_create(&p, nullptr, Productor, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    return 0;
}